from dao.BaseDao import BaseDao
from Static import Static


class SysDictDao(BaseDao):
    def initSysDict(self, df):
        self.__createTableSysDict()
        self.write2sql(df, Static.DB_SystemDict)

    def __createTableSysDict(self):
        sql = """
            CREATE TABLE IF NOT EXISTS `{}` (
                `id` int NOT NULL AUTO_INCREMENT,
                `dictLabel` VARCHAR ( 20 ),
                `dictValue` VARCHAR ( 20 ),
                `key` VARCHAR ( 256 ),
            PRIMARY KEY ( `id` ) 
            ) ENGINE = INNODB AUTO_INCREMENT = 1 DEFAULT CHARSET = utf8;""".format(
            Static.DB_SystemDict
        )
        self.executeNoResult(sql)

    def getDict(self, key):
        self.__createTableSysDict()
        sql_query = "SELECT `dictLabel`,`dictValue`,`key` from `{}` s ".format(
            Static.DB_SystemDict
        )
        if key != "":
            sql_query += "WHERE s.key='{}'".format(key)
        df = self.readSQL(sql_query)
        return df
